package cn.itrip.auth.controller;

import java.util.Calendar;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/*import cn.itrip.auth.exception.TokenValidationFailedException;*/
import cn.itrip.auth.service.TokenService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.vo.ItripTokenVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.ErrorCode;

/**
 * Token控制器
 * @author hduser
 *
 */
@Controller
@RequestMapping(value = "/api")
public class TokenController {

	@Resource
	private TokenService tokenService;
	/**
	 * 置换token
	 * 
	 * @return 新的token信息
	 */
	@ApiOperation(value = "客户端token置换", notes = "提供客户端置换token操作,服务器需要获取客户端header中的token串", tags = {""}, response = String.class)
	@ApiImplicitParam(value = "token",name="token",required = true, dataType = "String",paramType = "header")
	@RequestMapping(value = "/retoken", method = RequestMethod.POST,produces= "application/json")
	public @ResponseBody Dto replace(HttpServletRequest request) {
		String token= null;
		try {
			token = tokenService.reloadToken(request.getHeader("user-agent"),request.getHeader("token"));
			ItripTokenVO tokenVO=new ItripTokenVO(token,Calendar.getInstance().getTimeInMillis()+2*60*60*1000,Calendar.getInstance().getTimeInMillis());
			return DtoUtil.returnDataSuccess(tokenVO);
		} catch (Exception e) {
			e.printStackTrace();
			return DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_UNKNOWN);
		}
		/*
		 * 请求格式 
		 * $.ajax({
				headers:{
				          Accept:"application/json;charset=utf-8",
				          Content-Type:"application/json;charset=utf-8",
				          token:"lkfajsdlkfjawoier29"// 从cookie中获取
				},
				type："post",
				.....
				})
		 * 
		 */
	}

	/**
	 * 验证token是否有效
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "token验证", notes = "验证token是否存在", tags = {""}, response = String.class)
	@ApiImplicitParam(value = "token",name="token",required = true, dataType = "String",paramType = "header")
	@RequestMapping(value = "/validateToken", method = RequestMethod.GET,produces= "application/json",headers = "token")
	public @ResponseBody Dto validate(HttpServletRequest request) {
		try {
			boolean result=tokenService.validate(request.getHeader("user-agent"),request.getHeader("token"));
			if (result){
				return DtoUtil.returnSuccess("token有效");
			}else{
				return DtoUtil.returnSuccess("token无效");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_ILLEGAL_USERCODE);
		}
	}
}
